package top.appx.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import top.appx.entity.OrderEntity;
import top.appx.qo.OrderQO;
import top.appx.service.FoodService;
import top.appx.service.OrderService;
import top.appx.vo.FoodVO;
import top.appx.vo.OrderVO;
import top.appx.vo.Pager;
import top.appx.vo.ResultVO;

import javax.annotation.Resource;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.List;

/**
 * Created by john on 2015/7/12.
 */
@Controller
public class OrderController {

    @Resource
    private OrderService orderService;

    @Resource
    private FoodService foodService;

    @ResponseBody
    @RequestMapping(value="order/controller",params = "action=queryByPage")
    public Object queryByPage(OrderQO orderQO){
        Pager<OrderVO> pager =  orderService.queryByPage(orderQO);
        return pager;
    }

    @ResponseBody
    @RequestMapping(value="order/controller",params = "action=send")
    public Object send(OrderEntity order,HttpServletRequest request,HttpServletResponse response) throws UnsupportedEncodingException {
        order.setIp(request.getRemoteAddr());
        order.setStatus(1);
        int orderId = orderService.save(order);
        Cookie cookie = new Cookie("orderId",orderId+"");
        cookie.setMaxAge(60 * 60 * 2);//设置过期时间为两小时
        cookie.setPath("/");
        response.addCookie(cookie);


        cookie = new Cookie("oldRname",URLEncoder.encode(order.getRname(), "utf-8"));
        cookie.setMaxAge(9999999);
        cookie.setPath("/");
        response.addCookie(cookie);

        cookie = new Cookie("oldEmail",URLEncoder.encode(order.getEmail(), "utf-8"));
        cookie.setMaxAge(9999999);
        cookie.setPath("/");
        response.addCookie(cookie);

        return ResultVO.success();
    }

    @ResponseBody
    @RequestMapping(value = "order/controller",params = "action=save")
    public Object save(OrderEntity order){
        orderService.save(order);
        return ResultVO.success();
    }

    @ResponseBody
    @RequestMapping(value = "order/controller",params = "action=delById")
    public Object delById(int id){
        orderService.delById(id);
        return ResultVO.success();
    }

    @RequestMapping(value="prj/food/myOrder")
    public String myOrder(HttpServletRequest request){

        Cookie[] cookies = request.getCookies();
        int orderId = 0;
        if(cookies!=null){
            for(Cookie c : cookies){
                if(c.getName().equals("orderId")){
                    orderId = Integer.parseInt(c.getValue());
                }
            }
        }
        if(orderId!=0){
            OrderVO order = orderService.queryById(orderId);
            int foodId = order.getFood();
            FoodVO foodVO = foodService.queryById(foodId);
            order.setFoodVO(foodVO);
            request.setAttribute("order",order);
        }


        return "myOrder";
    }
    @RequestMapping(value="prj/food/allOrder")
    public String allOrder(HttpServletRequest request){
        List<OrderVO> orderList = orderService.allDealding();
        request.setAttribute("orderList",orderList);
        return "allOrder";
    }


    @ResponseBody
    @RequestMapping(value="test")
    public Object test(HttpServletResponse response){
        response.addCookie(new Cookie("aa","bbbb"));
        return ResultVO.success();
    }

    @ResponseBody
    @RequestMapping(value = "order/controller",params = "action=changStatus")
    public Object changeStatus(OrderEntity order){
        orderService.changeStatus(order);
        return ResultVO.success();
    }

}
